package base.interactive.http;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;

/**
 * Request
 *
 * @description: 请求接口
 * @author: cyj
 * @date 2020 -10-27 10:23:24
 */
public interface Request {

    /**
     * Exchange response entity
     *
     * @param <T>          parameter
     * @param url          url url
     * @param httpMethod   http method 请求类型
     * @param request      request 请求
     * @param responseType response type 响应类型
     * @return the response entity
     * @description:
     */
    <T> ResponseEntity<T> exchange(String url, HttpMethod httpMethod, HttpEntity<MultiValueMap<String,String>> request,
                                   Class<T> responseType);

    /**
     * Get response entity
     *
     * @param <T>          parameter
     * @param url          url
     * @param request      request
     * @param responseType response type
     * @return the response entity
     * @description:
     */
    default <T> ResponseEntity<T> get(String url, HttpEntity<MultiValueMap<String,String>> request,
                                      Class<T> responseType) {
        return exchange(url, HttpMethod.GET, request, responseType);
    }

    /**
     * Post response entity
     *
     * @param <T>          parameter
     * @param url          url
     * @param request      request
     * @param responseType response type
     * @return the response entity
     * @description:
     */
    default  <T> ResponseEntity<T> post(String url, HttpEntity<MultiValueMap<String,String>> request,
                                        Class<T> responseType) {
        return exchange(url, HttpMethod.POST, request, responseType);
    }

    /**
     * Put response entity
     *
     * @param <T>          parameter
     * @param url          url
     * @param request      request
     * @param responseType response type
     * @return the response entity
     * @description:
     */
    default <T> ResponseEntity<T> put(String url, HttpEntity<MultiValueMap<String,String>> request,
                                      Class<T> responseType) {
        return exchange(url, HttpMethod.PUT, request, responseType);
    }

    /**
     * Delete response entity
     *
     * @param <T>          parameter
     * @param url          url
     * @param request      request
     * @param responseType response type
     * @return the response entity
     * @description:
     */
    default <T> ResponseEntity<T> delete(String url, HttpEntity<MultiValueMap<String,String>> request,
                                         Class<T> responseType) {
        return exchange(url, HttpMethod.DELETE, request, responseType);
    }
}
